<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Login extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$d = array();
		$user_id = intval($this->session->admin_id);

		if (0 < $user_id) {
			$rs = $this->db->where('id', $user_id)->get('webmasters');
			$d['user'] = $rs->row_array();
			$d['logined'] = 1;
		}
		else {
			$d['logined'] = 0;
		}

		$this->load->view('login', $d);
	}

	public function verify()
	{
		$username = $this->input->post('username');
		$password = $this->input->post('password');

		if (!$username) {
			ajax_return(1, '用户名或密码错误');
		}

		if (!$password) {
			ajax_return(1, '用户名或密码错误');
		}

		$user = $this->db->where('username', $username)->get('webmasters')->row_array();

		if (!$user) {
			ajax_return(1, '用户名或密码错误');
		}

		if ($this->pwd->verify($password, $user['password'])) {
			$this->db->where('id', $user['id'])->set('password', $this->pwd->encrypt($password))->update('webmasters');
			$this->session->admin_id = $user['id'];
			$this->session->admin_name = $user['username'];
			ajax_return(0, '登录成功');
		}
		else {
			ajax_return(1, '登录失败');
		}
	}

	public function log_out()
	{
		$this->session->sess_destroy();
		redirect('login');
	}
}

?>
